package com.jiejie.controller;

import com.jiejie.commons.ServerResponse;
import com.jiejie.entity.Coupon;
import com.jiejie.entity.LoginCustomer;
import com.jiejie.entity.Movie;
import com.jiejie.service.ICouponService;
import com.jiejie.service.IMovieService;
import com.jiejie.util.TokenUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class IndexController {

    @Resource
    private IMovieService movieService;
@Resource
private ICouponService couponService;
    //1.加载首页数据
    @GetMapping("index")
    public ServerResponse loadIndexData(HttpServletRequest request){
        System.out.println(">>>>>>>>>>>>>>>>>>>");
        String token=request.getHeader("token");
        System.out.println("token:"+token);
        LoginCustomer loginCustomer =null;
        List<Coupon> couponList=null;
        if (!token.equals("null")) {
            //1.登陆用户数据
            loginCustomer = TokenUtil.parseToken(token);
            Integer customerId = loginCustomer.getCustomerId();
            String customerName = loginCustomer.getCustomerName();
            couponList=couponService.getCouponList();
        }
        //2.热门电影
        List<Movie> hotMovieList=movieService.getHotMovie();
        //3.预售电影
        List<Movie> exceptMovieList=movieService.getExpectMovie();
        System.out.println("============");
        System.out.println(hotMovieList);
        System.out.println(exceptMovieList);
        Map<String,Object> map=new HashMap<>();
        map.put("loginCustomer",loginCustomer);
        map.put("hotMovieList",hotMovieList);
        map.put("exceptMovieList",exceptMovieList);
        map.put("couponlist",couponList);
        return ServerResponse.success("ok",map);
    }



    @GetMapping("cinema")
    public ServerResponse cinemaMovie(HttpServletRequest request){
        String token=request.getHeader("token");
        System.out.println("token:"+token);
        LoginCustomer loginCustomer =null;
        if (!token.equals("null")) {
            //1.登陆用户数据
            loginCustomer = TokenUtil.parseToken(token);
        }
        //2.热门电影
        List<Movie> hotMovieList=movieService.getHotMovie();
        Map<String,Object> map=new HashMap<>();
        map.put("loginCustomer",loginCustomer);
        map.put("hotMovieList",hotMovieList);

        return ServerResponse.success("ok",map);
    }

}
